package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.GraySetCorpidListDTO;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/5/8 11:09
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX+"/script/graySet")
public class GraySetScriptController {

    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping(value = "/setCorpidList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setCorpidList(@RequestBody @Valid GraySetCorpidListDTO corpidListDTO, BindingResult br) throws XbbException {
        XbbResponse<Integer> response;
        if ( br.hasErrors() ) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            if (!proBaseConfig.getXbbCode().equals(corpidListDTO.getXbbCode())) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
            }
            String redisKey = "ab:policies:0:divdata";
            Set<String> grayCorpidList = new HashSet<>();
            List<String> corpidList = corpidListDTO.getCorpidList();
            CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.GRAY_CORPID_LIST.getAlias(), "0");
            if (Objects.isNull(companyConfigEntity)) {
                grayCorpidList.addAll(corpidList);
            } else {
                List<String> list = JSON.parseArray(companyConfigEntity.getConfigValue(), String.class);
                if (Objects.equals(0, corpidListDTO.getAdd())) {
                    list.removeAll(corpidList);
                } else {
                    list.addAll(corpidList);
                }
                grayCorpidList.addAll(list);
            }
            companyConfigModel.save("0", CompanyConfigEnum.GRAY_CORPID_LIST.getAlias(), CompanyConfigEnum.GRAY_CORPID_LIST.getName(), JSON.toJSONString(grayCorpidList));
            if (Objects.equals(0, corpidListDTO.getAdd())) {
                corpidList.forEach(item ->{
                    stringRedisTemplate.opsForHash().delete(redisKey, item);
                });
            } else {
                Map<Object, Object> map = new HashMap<>();
                grayCorpidList.forEach(item ->{map.put(item,"serverB");});
                stringRedisTemplate.opsForHash().putAll(redisKey, map);
            }
            response = new XbbResponse<>(1);
        }
        return JSON.toJSONString(response);
    }

}
